<template>
  <div class="article-list">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form :inline="true" :model="queryParams">
        <el-form-item label="标题">
          <el-input
            v-model="queryParams.keyword"
            placeholder="请输入文章标题"
            clearable
            @keyup.enter="handleSearch"
          />
        </el-form-item>
        <el-form-item label="分类">
          <el-select
            v-model="queryParams.category"
            placeholder="请选择分类"
            clearable
          >
            <el-option
              v-for="item in categories"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标签">
          <el-select
            v-model="queryParams.tags"
            placeholder="请选择标签"
            clearable
            multiple
            collapse-tags
          >
            <el-option
              v-for="item in tags"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>搜索
          </el-button>
          <el-button @click="resetQuery">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 文章列表 -->
    <el-table
      v-loading="loading"
      :data="articleList"
      border
      style="width: 100%"
    >
      <el-table-column prop="title" label="标题" min-width="200">
        <template #default="{ row }">
          <router-link 
            :to="{ name: 'ArticleDetail', params: { id: row.id }}"
            class="article-title"
          >
            {{ row.title }}
          </router-link>
        </template>
      </el-table-column>
      <el-table-column prop="category" label="分类" width="120" />
      <el-table-column prop="tags" label="标签" width="200">
        <template #default="{ row }">
          <el-tag
            v-for="tag in row.tags?.split(',').filter(Boolean)"
            :key="tag"
            size="small"
            class="tag-item"
          >
            {{ tag }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column 
        prop="createTime" 
        label="创建时间" 
        width="180"
        :formatter="(row) => formatDate(row.createTime)"
      />
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Search, Refresh } from '@element-plus/icons-vue'
import { getArticleList, getAllCategories, getAllTags } from '@/api/article'
import { formatDate } from '@/utils/format'
import type { ArticleQuery } from '@/api/types'

// 查询参数
const queryParams = ref<ArticleQuery>({
  pageNum: 1,
  pageSize: 10,
  keyword: '',
  category: '',
  tags: []
})

// 数据
const loading = ref(false)
const articleList = ref<any[]>([])
const total = ref(0)
const categories = ref<string[]>([])
const tags = ref<string[]>([])

// 获取文章列表
async function loadArticleList() {
  loading.value = true
  try {
    const { data } = await getArticleList(queryParams.value)
    articleList.value = data.records || []
    total.value = data.total || 0
  } catch (error) {
    console.error('Failed to load articles:', error)
    ElMessage.error('加载文章列表失败')
  } finally {
    loading.value = false
  }
}

// 加载分类和标签
async function loadFilters() {
  try {
    const [categoriesRes, tagsRes] = await Promise.all([
      getAllCategories(),
      getAllTags()
    ])
    categories.value = categoriesRes.data || []
    tags.value = tagsRes.data || []
  } catch (error) {
    console.error('Failed to load filters:', error)
    ElMessage.error('加载筛选条件失败')
  }
}

// 搜索
function handleSearch() {
  queryParams.value.pageNum = 1
  loadArticleList()
}

// 重置
function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    keyword: '',
    category: '',
    tags: []
  }
  loadArticleList()
}

// 分页
function handleSizeChange(val: number) {
  queryParams.value.pageSize = val
  loadArticleList()
}

function handleCurrentChange(val: number) {
  queryParams.value.pageNum = val
  loadArticleList()
}

// 初始化
onMounted(() => {
  loadArticleList()
  loadFilters()
})
</script>

<style scoped lang="scss">
.article-list {
  padding: 20px;

  .search-bar {
    margin-bottom: 20px;
  }

  .article-title {
    color: #409eff;
    text-decoration: none;
    
    &:hover {
      text-decoration: underline;
    }
  }

  .tag-item {
    margin-right: 5px;
  }

  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style> 